﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;
using Common;
using DataAccess;
using Util.PageInfo;
using ValidateCommon;
using System.Globalization;

namespace RichECMS.ServerPages.Clients
{
    public partial class List : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (State.LoginUser.ManagerCompetence > (int)Utility.ManagerCompetence.UserCanIssueAndClients)
            {
                Response.Write("您没有权限查看改页面");
                Response.End();
            }

            if (!IsPostBack)
            {
                State.strWhereSql = "";
                State.intPageIndex = 1;
                InitDataBind(State.intPageIndex, int.Parse(ddlShowCount.SelectedValue));
                AspNetPagerList.CurrentPageIndex = State.intPageIndex;
            }
        }

        /// <summary>
        /// 绑定
        /// </summary>
        private void InitDataBind(int intCurrentPage, int intPageSize)
        {
            //设置分页条数
            AspNetPagerList.PageSize = intPageSize;
            try
            {
                using (ClientsFactory objFactory = new ClientsFactory())
                {
                    StringBuilder strWhere = new StringBuilder();
                    BusinessLogic.ClassFactory.ClientsFactory objClientsFactory = new ClientsFactory();
                    BusinessLogic.BusinessRules.Clients objClients = objClientsFactory.NewInstance();

                    int intTotalCounts = 0;
                    if (intPageSize == 0)
                    {
                        intPageSize = objClients.GetTotalCount(" intWSID ="+State.intWSID);  //显示所有
                    }
                    int intPageFrom = intCurrentPage * intPageSize;

                    if (State.strWhereSql.Trim().Length == 0)
                    {
                        intTotalCounts = objClients.GetTotalCount(" intWSID =" + State.intWSID);
                    }
                    else
                    {
                        intTotalCounts = objClients.GetTotalCountByCustomSql(string.Format("SELECT count(*)  from Clients  where intWSID={0} {1} )", State.intSTID, State.strWhereSql));
                    }

                    LitPageCurrent.Text = intCurrentPage.ToString();
                    LitPageCount.Text = intTotalCounts.ToString();
                    if (intTotalCounts % intPageSize != 0)
                    {
                        if ((intCurrentPage == intTotalCounts / intPageSize + 1))
                        {
                            intPageFrom = (intCurrentPage - 1) * intPageSize;
                            intPageSize = intTotalCounts % intPageSize;
                            intPageFrom = intPageFrom + intPageSize;
                        }
                    }

                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " * from Clients " + " where  intWSID ="+State.intWSID+" " + State.strWhereSql + " order by dtmInTime desc,intClientsId desc)t order by  dtmInTime asc ,intClientsId asc)m " + " order by dtmInTime asc ,intClientsId asc)mm  order by dtmInTime desc ,intClientsId desc");

                    BusinessLogic.BusinessRules.Clients objClientsList = objClientsFactory.CreatorByFullSql(strWhere.ToString());
                    if (objClientsList != null)
                    {
                        rptList.DataSource = objClientsList.Data.Clients.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = intTotalCounts;
                        rptList.DataBind();
                        LitPageTotal.Text = AspNetPagerList.PageCount > 0 ? AspNetPagerList.PageCount.ToString() : "1";
                    }
                    else
                    {
                        rptList.DataSource = objFactory.NewInstance().Data.Clients.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = 0;
                        rptList.DataBind();
                        LitPageTotal.Text = AspNetPagerList.PageCount > 0 ? AspNetPagerList.PageCount.ToString() : "1";
                        //Alert("暂无数据！");
                    }
                    objClientsFactory.Dispose();
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButtonsearch(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbWhere = new StringBuilder();
            if (txtTitle.Value.Trim() != "")
            {
                sbWhere.AppendFormat(" and (chvClientsLoginID like '%{0}%' or chvClientsName like '%{0}% )", txtTitle.Value.Trim());
            }
            State.strWhereSql = sbWhere.ToString();
            State.intPageIndex = 1;
            InitDataBind(State.intPageIndex, int.Parse(ddlShowCount.SelectedValue));
        }

        /// <summary>
        /// 删除所选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteAll_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                using (DBManager objDBManager = new DBManager())
                {
                    objDBManager.BeginTransaction();
                    try
                    {
                        //文章表
                        ClientsFactory objFactory = new ClientsFactory(objDBManager);
                        objFactory.DeleteClientsWhere(" intClientsId in (" + SortHidden.Value.Trim() + ")");
                        //提交
                        objDBManager.Commit();
                        objFactory.Dispose();
                        AlertAndRedirect("信息删除成功", "../Section/RecycleList.aspx", false);
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 分页  PageChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPagerClients_PageChanged(object sender, EventArgs e)
        {
            State.intPageIndex = AspNetPagerList.CurrentPageIndex;
            InitDataBind(AspNetPagerList.CurrentPageIndex, int.Parse(ddlShowCount.SelectedValue));
        }

        /// <summary>
        /// rptList_ItemCommand  编辑   放入回收站  删除
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                using (DBManager objDBManager = new DBManager())
                {
                    int intClientsId = Convert.ToInt32(e.CommandArgument);
                    objDBManager.BeginTransaction();
                    try
                    {
                        //文章表
                        ClientsFactory objFactory = new ClientsFactory(objDBManager);
                        objFactory.DeleteClientsWhere(" intClientsID =" + intClientsId + "");

                        //提交
                        objDBManager.Commit();
                        objFactory.Dispose();
                        AlertAndRedirect("删除成功", "../clients/List.aspx", false);
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        return;
                    }
                }
            }
            //放入回收站
            if (e.CommandName == "Recycle")
            {
            }
        }


        //显示条数
        protected void ddlShowCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitDataBind(State.intPageIndex, int.Parse(ddlShowCount.SelectedValue));
        }
    }
}